﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class AdvImage : Portal.Interface.WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "AdvImage";
            }
        }

        public override bool IsMainWebPart
        {
            get
            {
                return true;
            }
        }


        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Ảnh quảng cáo");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }

    
        public override Portal.Interface.WebPartInt[] SubWebParts
        {
            get
            {
                Portal.Interface.WebPartInt[] SubWebParts = new Portal.Interface.WebPartInt[]{
                    new AddAdvImage(),
                    new UpdateAdvImage(),
                    new AdvUploadImage(),
                    new AddToAlbum(),
                    new ListItems(),
                    new ReAdvUploadImage(),
                    new AdvImageHtml(),
                    new AdvUploadAvatar()
                };
                return SubWebParts;
            }
        }


        public string GetViewImage(string belongSite, string Subject, string ShortId)
        {
            Subject = Environments.ConvertSubject(Subject);
            string Url = Environments.HttpRoot + "/" + Environments.AppendAlias + "-" + belongSite + "/" + Subject + "-vi-" + ShortId + "." + Environments.Extension;//?NewsId=" + ShortId;
            if (Environments.MultiLanguageInSite == 1)
            {
                Url = Url + "?lang=" + Environments.ActiveLanguage;
            }
            return Url;
        }

        [AjaxPro.AjaxMethod]
        public string GetHtml(string belongSite)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                Environments.CheckWebPartAllow(WebPartId, true);


                string SelectSite =
                      "<select id=drpSelectSiteForAdvImage onchange=javascript:FilterFunc();>";
                string[] Sites = Environments.GetSites();
                for (int iIndex = 0; iIndex < Sites.Length; iIndex++)
                {
                    if (Sites[iIndex].ToLower().Equals(belongSite))
                    {
                        SelectSite += "<option selected value=\"" + Sites[iIndex] + "\">" + Sites[iIndex] + "</option>";
                    }
                    else
                    {
                        SelectSite += "<option value=\"" + Sites[iIndex] + "\">" + Sites[iIndex] + "</option>";
                    }
                }
                SelectSite += "</select>";
                DataTable dtAdvImage = new AdvImageBLL().Reading(belongSite);
                string RetHtml =
                    "   <div>" + ResourceBLL.GetText(WebPartId, "SelectSite", "Site") + ":" + SelectSite + "&nbsp;<input type=button value=\"" + ResourceBLL.GetText(WebPartId, "Go", "Lọc dữ liệu") + "\" onclick=javascript:FilterFunc();>&nbsp;<input type=button onclick=\"javascript:ClientAddAdvImage();\" value=\"" + ResourceBLL.GetText(WebPartId, "TH_Add", "Thêm") + "\"></div>" +
                    "   <div id=divProcessing></div>";

                if (dtAdvImage.Rows.Count == 0)
                {
                    RetHtml += "<div class=\"cssNoData\">" + ResourceBLL.GetText(WebPartId, "NoData", "Chưa có dữ liệu nào trong chuyên mục này!") + "</div>";
                }
                else
                {
                    RetHtml +=
                        "   <table>" +
                        "       <tr>" +
                        "           <th>" + ResourceBLL.GetText(WebPartId, "TH_Subject", "Tiêu đề") + "</th>" +
                        "           <th></th>" +
                        "           <th></th>" +
                        "           <th></th>" +
                        "           <th></th>" +
                        "           <th></th>" +
                        "           <th></th>" +
                        "       </tr>";


                    for (int iIndex = 0; iIndex < dtAdvImage.Rows.Count; iIndex++)
                    {
                        string AdvImageId = (string)dtAdvImage.Rows[iIndex]["AdvImageId"];
                        string Subject = (string)dtAdvImage.Rows[iIndex]["Subject"];
                        string DeleteUrl = "javascript:ClientDeleteAdvImage('" + AdvImageId + "')";
                        string PermissionUrl = "javascript:ShowPermissionFuncForm('" + AdvImageId + "')";
                        string AddToAlbumUrl = "javascript:ShowAddToAlbumForm('" + AdvImageId + "')";
                        string ItemUrl = "javascript:ShowItems('" + AdvImageId + "')";
                        string SyncUrl = "javascript:ClientSyncAdvImage('" + AdvImageId + "')";
                        string LinkText = GetViewImage(belongSite, Subject, AdvImageId.Substring(0, 8));
                        LinkText = LinkText.Substring(LinkText.LastIndexOf("/") + 1);
                        LinkText = "<a target=\"_blank\" href=\"" + Environments.HttpRoot + "/" + Environments.AppendAlias + "-" + belongSite + "/" + LinkText + "\">" + LinkText + "</a>";

                        RetHtml +=
                        "       <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">" +
                        "           <td class=cssTdSubjectAdvImage>" + Subject + "</td>" +
                        "           <td><a href=\"javascript:ShowClientUpdateUrlForm('" + AdvImageId + "');\">" + ResourceBLL.GetText(WebPartId, "TH_Update", "Cập nhật") + "</a></td>" +
                        "           <td><a href=\"" + DeleteUrl + "\">" + ResourceBLL.GetText(WebPartId, "TH_Delete", "Xóa") + "</a></td>" +
                        "           <td><a href=\"" + PermissionUrl + "\">" + ResourceBLL.GetText(WebPartId, "TH_Permission", "Quyền") + "</a></td>" +
                        "           <td><a href=\"" + AddToAlbumUrl + "\">" + ResourceBLL.GetText(WebPartId, "TH_AddAlbum", "=> album") + "</a></td>" +
                        "           <td><a href=\"" + ItemUrl + "\">" + ResourceBLL.GetText(WebPartId, "TH_AddItems", "Ảnh") + "</a></td>" +
                        //"           <td><a href=\"" + SyncUrl + "\">" + ResourceBLL.GetText(WebPartId, "TH_Sync", "Sync") + "</a></td>" +
                        "       </tr>";
                    }
                    RetHtml +=
                        "   </table>";
                }
                return RetHtml;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(AdvImage));
            string ActiveSite = Portal.Environment.Environments.GetActiveSite();
            string RetHtml =
                Portal.Environment.WebPartUtilities.AttachJavascript("Portal.CMS", "AdvImage.js") +
                "<div id=divAdvImageMain class=cssDivAdvImage>" + GetHtml(ActiveSite) + "</div>";
            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public string GetAddNewUrl(string belongSite)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = FindPermissionType("AdvImageWebPart");
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "AddAdvImage", WebPartId, null, true);

                return Portal.Environment.Screen.BuildDialogUrl(new ParamCls[]{
                    new ParamCls("WebPartId",new AddAdvImage().WebPartId),
                    new ParamCls("belongSite",belongSite)
            });
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetConfirmDeleteMsg()
        {
            return ResourceBLL.GetText(WebPartId,"ConfirmDeleteMsg","Bạn đã chắc chắn xóa chưa?");
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideCheckAllowDelete(string AdvImageId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = FindPermissionType("AdvImage");
                AdvImageCls AdvImageCls = new AdvImageBLL().CreateModel(Environments.ActiveLanguage, AdvImageId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "DeleteAdvImage", AdvImageId, AdvImageCls.Owner, true);

                return "";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideDeleteAdvImage(string AdvImageId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                ServerSideCheckAllowDelete(AdvImageId);
                SqlTransaction sqlTransaction = Environment.Environments.CreateTransaction();
                try
                {
                    new AdvImageBLL().DeleteAdvImage(sqlTransaction, AdvImageId);
                    sqlTransaction.Commit();
                }
                catch (Exception ex)
                {
                    sqlTransaction.Rollback();
                    throw (ex);
                }
                return ResourceBLL.GetText(WebPartId,"DeleteOkMessage","Xóa thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetShowUpdateForm(string AdvImageId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = FindPermissionType("AdvImage");
                AdvImageCls AdvImageCls = new AdvImageBLL().CreateModel(Environments.ActiveLanguage, AdvImageId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "UpdateAdvImage", AdvImageId, AdvImageCls.Owner, true);

                string UpdateUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new UpdateAdvImage().WebPartId),
                        new ParamCls("AdvImageId",AdvImageId)
                    });
                return UpdateUrl;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }




        public override Portal.Interface.FunctionCls[] FunctionList
        {
            get
            {
                return new FunctionCls[]
                {
                    new FunctionCls("ViewAdvImage",ResourceBLL.GetText(WebPartId,"ViewAdvImage","Xem nhóm ảnh quảng cáo"),WebPartId),
                    new FunctionCls("AddAdvImage",ResourceBLL.GetText(WebPartId,"AddAdvImage","Thêm mới nhóm ảnh quảng cáo"),WebPartId),
                    new FunctionCls("UpdateAdvImage",ResourceBLL.GetText(WebPartId,"UpdateAdvImage","Cập nhật nhóm ảnh quảng cáo"),WebPartId),

                    new FunctionCls("AddAdvImageItem",ResourceBLL.GetText(WebPartId,"AddAdvImageItem","Thêm mới ảnh quảng cáo vào nhóm"),WebPartId),
                    
                    new FunctionCls("ShowAdvImageItems",ResourceBLL.GetText(WebPartId,"ShowAdvImageItems","Xem chi tiết ảnh"),WebPartId),
                    new FunctionCls("DeleteAdvImageItem",ResourceBLL.GetText(WebPartId,"DeleteAdvImageItem","Xóa chi tiết"),WebPartId),
                    new FunctionCls("PermissionAdvImage",ResourceBLL.GetText(WebPartId,"PermissionAdvImage","Phân quyền nhóm ảnh quảng cáo"),WebPartId),
                    new FunctionCls("PermissionAdvImageItem",ResourceBLL.GetText(WebPartId,"PermissionAdvImageItem","Phân quyền cho từng bản ghi nhóm ảnh quảng cáo"),WebPartId),
                    new FunctionCls("ReUploadAdvImageItem",ResourceBLL.GetText(WebPartId,"ReUploadAdvImageItem","Cập nhật lại ảnh quảng cáo"),WebPartId),

                    new FunctionCls("DeleteAdvImage",ResourceBLL.GetText(WebPartId,"DeleteAdvImage","Xóa nhóm ảnh quảng cáo"),WebPartId),
                    new FunctionCls("AddToAlbum",ResourceBLL.GetText(WebPartId,"AddToAlbum","Đưa vào album ảnh"),WebPartId),
                    new FunctionCls("Publishing",ResourceBLL.GetText(WebPartId,"Publishing","Phát hành lên website public"),WebPartId),
                    new FunctionCls("UnPublishing",ResourceBLL.GetText(WebPartId,"UnPublishing","Ngưng phát hành lên website public"),WebPartId),
                    new FunctionCls("SaveAdvImageItem",ResourceBLL.GetText(WebPartId,"SaveAdvImageItem","Ghi lại ảnh quảng cáo"),WebPartId),
                    new FunctionCls("SyncWithServer",ResourceBLL.GetText(WebPartId,"SyncWithServer","Cập nhật lại máy chủ public"),WebPartId),
                };
            }
        }

        public override Portal.Interface.PermissionTypeCls[] PermissionTypes
        {
            get
            {
                return new PermissionTypeCls[]{
                    new PermissionTypeCls("AdvImageItems","Ảnh quảng cáo website (Items)",new string[]{"PermissionAdvImageItem","DeleteAdvImageItem","Publishing","UnPublishing","SaveAdvImageItem","ReUploadAdvImageItem"}),
                    new PermissionTypeCls("AdvImage","Ảnh quảng cáo website",new string[]{"PermissionAdvImage","AddAdvImageItem","SyncWithServer","ShowAdvImageItems","AddToAlbum","DeleteAdvImage"}),
                    new PermissionTypeCls("AdvImageWebPart","Ảnh quảng cáo website",new string[]{"AddAdvImage","PermissionAdvImage"})
                };
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetShowFormSetPermission(string AdvImageId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = FindPermissionType("AdvImage");
                AdvImageCls AdvImageCls = new AdvImageBLL().CreateModel(Environments.ActiveLanguage, AdvImageId);

                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "PermissionAdvImage", AdvImageId, AdvImageCls.Owner, true);
                
                return Environment.Permission.GetPermissionUrl(WebPartId, AdvImageId, "AdvImage");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }
        
        
        [AjaxPro.AjaxMethod]
        public string GetAddToAlbumForm(string AdvImageId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);

                PermissionTypeCls PermissionTypeCls = FindPermissionType("AdvImage");
                AdvImageCls AdvImageCls = new AdvImageBLL().CreateModel(Environments.ActiveLanguage, AdvImageId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "AddToAlbum", AdvImageId, AdvImageCls.Owner, true);

                return Screen.BuildDialogUrl(new AddToAlbum().WebPartId,new ParamCls[]{
                    new ParamCls("AdvImageId",AdvImageId)
                });
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetItemsUrl(string AdvImageId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);

                AdvImageCls AdvImageCls = new AdvImageBLL().CreateModel(Environments.ActiveLanguage, AdvImageId);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new AdvImage().PermissionTypes, "AdvImage");
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "ShowAdvImageItems", AdvImageId, AdvImageCls.Owner, true);

                return Screen.BuildDialogUrl(new ListItems().WebPartId, new ParamCls[]{
                    new ParamCls("AdvImageId",AdvImageId)
                });
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        public override PermissionTypeCls GetServicePermissionType()
        {
            return PermissionTypes[2];
        }



        [AjaxPro.AjaxMethod]
        public string GetProcessingText()
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                string Url = Environment.Environments.GetActiveThemeUrl() + "/Global/Loading.gif";
                return "<img src=\"" + Url + "\"><a class=cssProcessing>" + ResourceBLL.GetText(WebPartId, "ProcessingText", "Đang đọc dữ liệu...") + "</a>";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmSyncAllMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmSyncAllMsg", "Bạn đã chắc chắn update lại dữ liệu tin tức trên máy chủ từ máy local chưa?");
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideSyncAdvImage(string AdvImageId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                new AdvImageBLL().SyncAdvImage(AdvImageId);
                return GetText("SyncOk", "Đồng bộ với máy chủ website thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }
    }
}
